<template>
  <div class="auth-form-container">
    <h2 class="title">{{ title }}</h2>
    <el-form
      :model="form"
      :rules="rules"
      ref="formRef"
      label-width="80px"
      class="auth-form"
    >
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username" placeholder="请输入账号" clearable />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          type="password"
          placeholder="请输入密码"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </el-form-item>
      <el-form-item>
        <slot name="additional-links"></slot>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

const props = defineProps({
  title: String,
  onSubmit: Function,
});

const form = reactive({
  username: "",
  password: "",
});

const rules = {
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const formRef = ref(null);
const router = useRouter();

const handleSubmit = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      props.onSubmit(form);
    } else {
      ElMessage.error("验证失败，请检查输入！");
    }
  });
};
</script>

<style scoped>
.auth-form-container {
  width: 400px;
  padding: 30px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.title {
  margin-bottom: 20px;
  color: #333;
  font-size: 24px;
  font-weight: bold;
}

.auth-form {
  margin-top: 10px;
}
</style>
